package com.guomei.controller.settings;

import com.guomei.bean.settings.vo.PageFooterSetting;
import com.guomei.service.settings.SettingService;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 页脚配置控制器
 */
@RestController
@RequestMapping("/api1/settings/page-footer")
@Slf4j
public class PageFooterSettingController {
    
    @Resource
    private SettingService settingService;
    
    /**
     * 获取页脚配置
     * @return 页脚配置信息
     */
    @GetMapping
    public Result<PageFooterSetting> getPageFooterSetting() {
        try {
            PageFooterSetting pageFooterSetting = settingService.getPageFooterSetting();
            return Result.success(pageFooterSetting);
        } catch (Exception e) {
            log.error("获取页脚配置失败", e);
            return Result.error("获取页脚配置失败：" + e.getMessage());
        }
    }
    
    /**
     * 创建或更新页脚配置
     * @param pageFooterSetting 页脚配置信息
     * @return 操作结果
     */
    @PostMapping
    public Result<String> saveOrUpdatePageFooterSetting(@RequestBody PageFooterSetting pageFooterSetting) {
        try {
            boolean success = settingService.saveOrUpdatePageFooterSetting(pageFooterSetting);
            if (success) {
                return Result.success("保存页脚配置成功");
            } else {
                return Result.error("保存页脚配置失败");
            }
        } catch (Exception e) {
            log.error("保存页脚配置失败", e);
            return Result.error("保存页脚配置失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新页脚配置（PUT方式）
     * @param pageFooterSetting 页脚配置信息
     * @return 操作结果
     */
    @PutMapping
    public Result<String> updatePageFooterSetting(@RequestBody PageFooterSetting pageFooterSetting) {
        try {
            if (!settingService.existsPageFooterSetting()) {
                return Result.error("页脚配置不存在，请先创建");
            }
            boolean success = settingService.saveOrUpdatePageFooterSetting(pageFooterSetting);
            if (success) {
                return Result.success("更新页脚配置成功");
            } else {
                return Result.error("更新页脚配置失败");
            }
        } catch (Exception e) {
            log.error("更新页脚配置失败", e);
            return Result.error("更新页脚配置失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除页脚配置
     * @return 操作结果
     */
    @DeleteMapping
    public Result<String> deletePageFooterSetting() {
        try {
            boolean success = settingService.deletePageFooterSetting();
            if (success) {
                return Result.success("删除页脚配置成功");
            } else {
                return Result.error("删除页脚配置失败或配置不存在");
            }
        } catch (Exception e) {
            log.error("删除页脚配置失败", e);
            return Result.error("删除页脚配置失败：" + e.getMessage());
        }
    }
    
    /**
     * 检查页脚配置是否存在
     * @return 是否存在
     */
    @GetMapping("/exists")
    public Result<Boolean> existsPageFooterSetting() {
        try {
            boolean exists = settingService.existsPageFooterSetting();
            return Result.success(exists);
        } catch (Exception e) {
            log.error("检查页脚配置是否存在失败", e);
            return Result.error("检查页脚配置是否存在失败：" + e.getMessage());
        }
    }
}

